let id = getQueryVariable("id");

function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    console.log(query);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] === variable){return pair[1];}
    }
    return(false);
}

var app = new Vue({
    el:'#app',
    data:{
        question:
            {
                title:'',
                user_id:"",
                user:{


                },
                subject:'',
                createTime:'',
                answers:[
                    {
                        id:0,
                        userId:'',
                        user: {

                        },
                        content: '',
                        createTime: '',
                        reviewNum: '',
                        comments:[
                            {
                                user:{

                                },
                                content:'',
                                createTime: '',
                                comments: [
                                    {

                                        content:'',
                                    }
                                ]
                            }

                        ],
                    }
                ],
                content:'',
                ansNum: '',
            },
            inputAnswer:null,
        },

    components: {
        'editor':Editor
    },

    methods:{
            addAnswer:function (){
                console.log(this.inputAnswer);
                if (this.inputAnswer===null){
                    alert("回答不可为空！");
                    return;
                }
                var current_user_id = localStorage.getItem('Authorization').split('_')[0];
                axios.post('http://localhost:8095/addAnswer',
                    {
                        questionid:id,
                        userid:current_user_id,
                        content:this.inputAnswer,
                    },
                    {
                        headers:{
                            // @RequestParam需要使用如下这种type进行数据传输
                            'Content-Type':'application/x-www-form-urlencoded',
                        },
                        // 这个其实是在模仿qs库对传输的data进行处理，也可以直接调用qs库
                        // 见 https://blog.csdn.net/u011332271/article/details/100515870
                        // https://blog.csdn.net/qq_43681948/article/details/102815325
                        transformRequest: [
                            function(data) {
                                let ret = "";
                                for (let it in data) {
                                    ret +=
                                        encodeURIComponent(it) +
                                        "=" +
                                        encodeURIComponent(data[it]) +
                                        "&";
                                }
                                return ret;
                            }
                        ],
                    })
                location.href='#answer_block';
                location.reload();
            }
        },
        created:function (){
            axios.get('http://localhost:8095/question/'+id).then(
                (response)=>{
                    console.log(response);
                    this.question = response.data;
                }
            );
        }
    }
)
Vue.component("editor",{
    template:"#editor-template",
})
Vue.component("tree-item", {
    template: "#item-template",
    props: {
        item: Object
    },
});